//geojson服务
function GeoJsonService(layerInfo) {
    var GeoJsonService = new Object;
    GeoJsonService.id = layerInfo.hasOwnProperty("GUID") ? layerInfo.GUID : "";
    GeoJsonService.style = layerInfo.hasOwnProperty("STYLE") ? layerInfo.STYLE : "#0033FF";
    GeoJsonService.lineWidth = layerInfo.hasOwnProperty("WIDTH") ? layerInfo.WIDTH : 5;
    GeoJsonService.speed = layerInfo.hasOwnProperty("SPEED") ? layerInfo.SPEED : 1;
    GeoJsonService.pointColor = layerInfo.hasOwnProperty("pointColor") ? layerInfo.pointColor : "#FFFFFF";
    GeoJsonService.url = layerInfo.hasOwnProperty("URL") ? layerInfo.URL : "";
    GeoJsonService.name = layerInfo.hasOwnProperty("NAME") ? layerInfo.NAME : "";
    GeoJsonService.CreateLayer = function() {
        var url = this.url;
        var thisLayer;
        var that = this;
        var geojsontype = null; //geojson类型
        var features = null;
        var allPoints = [];
        var labels = viewer.scene.primitives.add(new Cesium.LabelCollection());
        $.get(url, function(data) {
            var josnN = typeof data == 'string' ? JSON.parse(data) : data;
            features = josnN.features;
            geojsontype = features[0].geometry.type;
        })
        let config = {
            'one': 10000,
            'two': 3000,
            'three': 600
        };

        let fontcolor = {
            'one': new Cesium.Color.fromCssColorString("#ff0000").withAlpha(1),
            'two': new Cesium.Color.fromCssColorString("#274e13").withAlpha(1),
            'three': new Cesium.Color.fromCssColorString("#00369a").withAlpha(1)
        };

        let configcolor = {
            "重点开发区": new Cesium.Color.fromCssColorString("#f16522").withAlpha(1),
            "重点生态功能区": new Cesium.Color.fromCssColorString("#197b30").withAlpha(1),
            "农产品主产区": new Cesium.Color.fromCssColorString("#f7941d").withAlpha(1),
            "": new Cesium.Color.fromCssColorString("#f7941d").withAlpha(1),
        };


        this.getDegreesArrayHeights = function(feature) {
            let degreesArrayHeights = [];
            let coordinates;
            if (feature.geometry.type == "MultiPolygon") {
                coordinates = feature.geometry.coordinates[0][0];
            } else if (feature.geometry.type == "Polygon") {
                coordinates = feature.geometry.coordinates[0];
            }
            //坐标串转为需要的格式[x,y,z,x,y,z....]
            for (let i = 0; i < coordinates.length; i++) {
                const element = coordinates[i];
                degreesArrayHeights.push(element[0]);
                degreesArrayHeights.push(element[1]);
                degreesArrayHeights.push(100);
            }
            return degreesArrayHeights;
        }
        if (geojsontype.indexOf("Point") > -1) {
            //点
            var positions = null;
            for (let index = 0; index < features.length; index++) {
                positions = Cesium.Cartographic.fromDegrees(features[index].geometry.coordinates[0], features[index].geometry.coordinates[1]);
                allPoints.push(positions);
            }
            var promise2 = Cesium.sampleTerrain(viewer.terrainProvider, 15, allPoints);
            Cesium.when(promise2, function(updatedPositions) {
                var distancetype = null;
                for (let i = 0; i < features.length; i++) {
                    if (!updatedPositions[i].height) {
                        updatedPositions[i].height = 0;
                    }
                    if (features[i].properties.Type) {
                        distancetype = config[features[i].properties.Type];
                    } else {
                        distancetype = 3000;
                    }
                    labels.add({
                        position: new Cesium.Cartesian3.fromDegrees(features[i].geometry.coordinates[0], features[i].geometry.coordinates[1], updatedPositions[i].height + 50),
                        text: features[i].properties.NAME,
                        scale: 0.5,
                        font: 'normal 30px MicroSoft YaHei',
                        scaleByDistance: new Cesium.NearFarScalar(10000, 1, 100000, 0.5),
                        style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                        fillColor: fontcolor[features[i].properties.Type],
                        horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                        verticalOrigin: Cesium.VerticalOrigin.CENTER,
                        distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, distancetype),
                        heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
                        outlineWidth: 3,
                        outlineColor: Cesium.Color.BLACK
                    })
                }
            });
            thisLayer = labels;
            // viewer.dataSources.add(thisLayer);
            thisLayer.NAME = that.name;
            cesium.layer3DList[that.id] = thisLayer;

        } else if (geojsontype.indexOf("Polygon") > -1) {
            let feature;
            let collection = new Cesium.PrimitiveCollection();
            let labels = new Cesium.LabelCollection();

            collection.add(labels);
            viewer.scene.primitives.add(collection);
            for (let i = 0; i < features.length; i++) {
                feature = features[i];
                let degreesArrayHeights = this.getDegreesArrayHeights(feature);
                let height = feature.properties.DCMJ + 1500;
                let polygon = new Cesium.PolygonGeometry({
                    polygonHierarchy: new Cesium.PolygonHierarchy(
                        Cesium.Cartesian3.fromDegreesArrayHeights(degreesArrayHeights)),
                    perPositionHeight: false,
                    extrudedHeight: height,
                    vertexFormat: Cesium.VertexFormat.ALL
                });
                let geometry = Cesium.PolygonGeometry.createGeometry(polygon);
                //primitive方式
                let instance = new Cesium.GeometryInstance({
                    geometry: geometry
                });
                collection.add(new Cesium.Primitive({
                    geometryInstances: instance,
                    appearance: new xt3d.PolygonObject.PrimitiveGradientAppearance(configcolor[feature.properties.ZTGNQ]),
                }));

                let center = turf.center(feature);
                let point = center.geometry.coordinates;
                let position = Cesium.Cartesian3.fromDegrees(point[0], point[1], height);
                labels.add({
                    position: position,
                    text: feature.properties.ZTGNQ,
                    fillColor: Cesium.Color.WHITE,
                    scale: 0.5,
                    font: 'normal 60px MicroSoft YaHei',
                    distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 2000000),
                    verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                    scaleByDistance: new Cesium.NearFarScalar(50000, 1, 500000, 0.5),
                    style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                    pixelOffset: new Cesium.Cartesian2(0, -10),
                    outlineWidth: 20,
                    outlineColor: Cesium.Color.BLACK
                });


            }
            thisLayer = collection;
            // viewer.dataSources.add(thisLayer);
            thisLayer.NAME = that.name;
            cesium.layer3DList[that.id] = thisLayer;
        } else {
            Cesium.GeoJsonDataSource.load(url, {
                clampToGround: true,
            }).then(function(dataSource) {
                var entities = dataSource.entities.values;
                for (var i = 0; i < entities.length; i++) {
                    let entity = entities[i];
                    entity.polyline.material = Cesium.Color.fromCssColorString('#c500ff');
                    entity.polyline.width = 6;
                }
                thisLayer = dataSource;
                viewer.dataSources.add(thisLayer);
                thisLayer.NAME = that.name;
                cesium.layer3DList[that.id] = thisLayer;
            }.bind(this))
            return {};
            //线
        }
    }
    return GeoJsonService;
}